<template>
  <div>
    <div>
      <h4 class="font-weight-bold">工具执行日志</h4>
      <h6 class="font-weight-bold">企业客户：{{customerName}}</h6>
    </div>

    

    <sky-table 
    :data="table.data" 
    :columns="table.columns" 
    :show-index="table.showIndex" 
    class="mt-0"></sky-table>
  </div>
</template>

<script>
import Vue from "vue";

function padLeftZero (str) {
    return ('00' + str).substr(str.length);
};

function formatDate (longStr, fmt) {
   let date = new Date(longStr);

    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};



export default {
    name: "tables-vue-tables-2",
    metaInfo: {
        title: "Vue Tables 2 - Tables"
    },
    components: {
    },
    data() {
        
        return {
            customerName:"",
        table: {
          data: [],
          columns: [
            
            {
              title: "工具类型",
              key: "riskToolType",
              align: "center",
              render: (h, params) => {
                  let toolTypeName = "未知";
                    if(params.row.riskToolType == "alertRule"){
                        toolTypeName = "预警规则";
                    }else if(params.row.riskToolType == "scoreCard") {
                        toolTypeName = "打分卡";
                    }
                    return (
                        <div>
                            {toolTypeName}
                        </div>
                    );
                }
            },
            {
              title: "工具名称",
              key: "riskToolName"
            },           
            {
              title: "执行来源",
              key: "runType",
              render: (h, params) => {
                  let runTypeName = "未知";
                    if(params.row.runType == "manual"){
                        runTypeName = "手动";
                    }else if(params.row.runType == "job") {
                        runTypeName = "Job";
                    }else if(params.row.runType == "api") {
                        runTypeName = "api调用";
                    }
                    return (
                        <div>
                            {runTypeName}
                        </div>
                    );
                }
            },
            {
              title: "执行状态",
              key: "status",
              render: (h, params) => {
                  let statusName;
                   if(params.row.status == 0){
                        statusName = "执行准备中";
                    }else if(params.row.status == 1){
                        statusName = "执行成功";
                    }else if(params.row.status == 2){
                        statusName = "执行失败";
                    }
                    return (
                        <div>
                            {statusName}
                        </div>
                    );
                }
            },
            {
              title: "执行结果",
              key: "runResult",
              render: (h, params) => {
                  let runResult = params.row.runResult;
                  if(params.row.status == 2){
                    runResult = params.row.errorMessage;
                  }else if(params.row.status == 1){
                    if(params.row.riskToolType == "scoreCard"){
                        runResult = "得分："+params.row.finalScore;
                    }else if(params.row.riskToolType == "alertRule"){
                        runResult = params.row.okMessage;
                    }
                  }

                    return (
                        <div>
                            {runResult}
                        </div>
                    );
                }
            },
            {
              title: "执行时间",
              width:150,
              key: "createDate",
              render: (h, params) => {
                    let createDate = formatDate(params.row.createDate,'yyyy-MM-dd hh:mm:ss');
                    return (
                        <div>
                            {createDate}
                        </div>
                    );
                }
            },
            {
              title: "金融产品",
              
              key: "riskServiceName"
            },
            {
              title: "风控节点",
              key: "riskServiceNodeName"
            },
            {
              title: "操作",
              key: "",
              width: 120,
              align: "center",
              render: (h, params) => {
                  let vm = this;
                //   console.log(vm);
                  return (
                      <div>                          
                          <i-button
                              type="text"
                              size="small"
                              class="mr-2"
                              shape="circle"
                              icon="document-text"
                              onClick={() => {
                                  return vm.toNodeRunLog(params);
                              }}
                          >
                          </i-button>
                      </div>
                  );
              }
            }
          ]
        }
    }},
    filters: {
        formatDate(time) {
        var date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd');
      }
    },
    created() {
        let self = this;
        
    },
    mounted() {
        this.oid = this.$route.query.oid;
        this.customerName = this.$route.query.customerName;
        this.loadToolRunLogData();
    },
    methods: {
        toNodeRunLog: function(params) {
            let self = this;
            var url;
            if(params.row.riskToolType == 'scoreCard'){
                url = '/customer/scoreCard/runResultDetail';
            }else if (params.row.riskToolType == 'alertRule'){
                url = '/customer/alertRule/runResultDetail';
            }
            self.$router.push({
                path: url,
                query: {
                    oid: params.row.oid,
                    riskToolName: params.row.riskToolName,
                    customerName: self.customerName,
                    finalScore: params.row.finalScore
                }
            })
        },
        remove(row) {
            alert(`Remove: ${row.first_name} ${row.last_name}`);
        },
        loadToolRunLogData() {
            let self = this;

            let url = "/app/v1/toolRunLogs?pageSize=1000&riskObjectType=customer&riskObjectOid="+self.oid;

            self.$http.get(url).then(res => {
                // console.log(res);                
                self.table.data = res.data.data;
            });
        }
    }
};
</script>
